home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / Book Chapters / 09 - QuickDraw 3D / Mesh / Mesh.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-21  |  5.3 KB  |  220 lines  |  [TEXT/MPCC]

  1. /****************************/
  2. /*               MESH.C            */
  3. /****************************/
  4.  
  5.  
  6. /****************************/
  7. /*    EXTERNALS             */
  8. /****************************/
  9.  
  10. #include <qd3d.h>
  11. #include <QD3DGeometry.h>
  12. #include <QD3DSet.h>
  13. #include <QD3DView.h>
  14. #include <QD3DShader.h>
  15. #include <QD3DGroup.h>
  16. #include <QD3DRenderer.h>
  17. #include <QD3DAcceleration.h>
  18. #include <QD3DDrawContext.h>
  19.  
  20. extern    TQ3ViewObject        gMyViewObject;
  21. extern    TQ3StyleObject        gMyStyleObject_Interpolation;
  22. extern    TQ3StyleObject        gMyStyleObject_Backfacing;
  23. extern    TQ3StyleObject        gMyStyleObject_Fillstyle;
  24. extern    TQ3ShaderObject        gMyShaderObject_IlluminationShader;
  25.  
  26.  
  27.  
  28. /****************************/
  29. /*    PROTOTYPES            */
  30. /****************************/
  31.  
  32. extern    void DoFatalAlert(Str255 s);
  33.  
  34. void DrawMyObjects(void);
  35. void CreateMyMesh(void);
  36.  
  37.  
  38. /****************************/
  39. /*    CONSTANTS             */
  40. /****************************/
  41.  
  42.  
  43. /*********************/
  44. /*    VARIABLES      */
  45. /*********************/
  46.  
  47. TQ3GeometryObject        gMyMeshObject;
  48.  
  49.  
  50. /****************** CREATE MY MESH ******************/
  51. //
  52. // Creates a mesh geometry object.
  53. //
  54. // INPUT    : none
  55. // OUTPUT    : none
  56. //
  57.  
  58. void CreateMyMesh(void)
  59. {
  60. long                i,faceNum,vertexNum;
  61. float                ambient = 1.0;
  62. TQ3ColorRGB            color;
  63. TQ3AttributeSet        vertexAttribs;
  64. TQ3Vertex3D            vertex;
  65. TQ3MeshFace            face;
  66. TQ3MeshVertex        meshVertexIDList[16];
  67. TQ3MeshVertex        faceVerts[4];
  68. TQ3GeometryObject    meshObj;
  69.  
  70. TQ3Point3D            meshVertexCoords[16] =
  71.                     {
  72.                         -10,0,-10,    -5,0,-10,    5,0,-10,    10,0,-10,
  73.                         -10,0,-5,    -5,0,-5,    5,0,-5,        10,0,-5,
  74.                         -10,0,5,    -5,0,5,        5,0,5,        10,0,5,
  75.                         -10,0,10,    -5,0,10,    5,0,10,        10,0,10
  76.                     };
  77.  
  78. long                meshFaceVertices[9*4] =
  79.                     {
  80.                         0,4,5,1,    1,5,6,2,    2,6,7,3,
  81.                         4,8,9,5,    5,9,10,6,    6,10,11,7,
  82.                         8,12,13,9,    9,13,14,10,    10,14,15,11
  83.                     };
  84.  
  85.  
  86.             /* CREATE NEW MESH OBJECT */
  87.             
  88.     meshObj = Q3Mesh_New();
  89.     if (meshObj == nil)
  90.         DoFatalAlert("\pError making new mesh!");
  91.         
  92.  
  93.             /***********************************/
  94.             /* CREATE THE VERTICES OF THE MESH */
  95.             /***********************************/
  96.  
  97.     for (vertexNum = 0; vertexNum < 16; vertexNum++)
  98.     {
  99.             /* MAKE ATTRIBUTES FOR THIS VERTEX */
  100.  
  101.         vertexAttribs = Q3AttributeSet_New();    // make attribs
  102.         if (vertexAttribs == nil)
  103.             DoFatalAlert("\pError making new vertex attribs.");
  104.         
  105.         color.r = (float)1/((Random()&0xf)+1);    // make random RGB color
  106.         color.g = (float)1/((Random()&0xf)+1);    // for each vertex
  107.         color.b = (float)1/((Random()&0xf)+1);
  108.  
  109.         Q3AttributeSet_Add(vertexAttribs,         // set color
  110.                 kQ3AttributeTypeDiffuseColor, &color);
  111.                 
  112.         Q3AttributeSet_Add(vertexAttribs,         // set ambient
  113.                 kQ3AttributeTypeAmbientCoefficient, &ambient);
  114.  
  115.  
  116.                 /* SET COORDS & ATTRIBS FOR VERTEX */
  117.  
  118.         vertex.point.x = meshVertexCoords[vertexNum].x;    // copy x,y,z coords
  119.         vertex.point.y = meshVertexCoords[vertexNum].y;
  120.         vertex.point.z = meshVertexCoords[vertexNum].z;
  121.         vertex.attributeSet = vertexAttribs;            // attach attribs
  122.  
  123.  
  124.             /* CREATE A NEW VERTEX & KEEP THE RETURNED VERTEX ID */
  125.  
  126.         meshVertexIDList[vertexNum]  = Q3Mesh_VertexNew(meshObj, &vertex);
  127.         
  128.  
  129.             /* DISPOSE OF OUR REFERENCE TO THE ATTRIBUTE SET */
  130.  
  131.         Q3Object_Dispose(vertexAttribs);
  132.     }        
  133.  
  134.             /*****************************/
  135.             /* CREATE MESH POLYGON FACES */
  136.             /*****************************/
  137.                         
  138.     for (i = 0, faceNum=0; faceNum < 9; faceNum++)
  139.     {
  140.                 /* GET VERTEX ID’S FROM TABLE */
  141.  
  142.         faceVerts[0] = meshVertexIDList[meshFaceVertices[i++]];    // copy vertex
  143.         faceVerts[1] = meshVertexIDList[meshFaceVertices[i++]];    // ID’S into local
  144.         faceVerts[2] = meshVertexIDList[meshFaceVertices[i++]];    // array for easy
  145.         faceVerts[3] = meshVertexIDList[meshFaceVertices[i++]];    // use.
  146.  
  147.  
  148.                 /* MAKE FACE WITH NO ATTRIBUTES */
  149.  
  150.         face = Q3Mesh_FaceNew(meshObj,4,&faceVerts[0],nil);        // make the face
  151.         if (face == nil)
  152.             DoFatalAlert("\pError adding face to mesh object!");
  153.     }
  154.     
  155.     gMyMeshObject = meshObj;
  156. }
  157.  
  158.  
  159. /******************* DRAW MY OBJECTS *********************/
  160. //
  161. // Draws a frame of animation for the FlyThru window.
  162. //
  163.  
  164. void DrawMyObjects(void)
  165. {
  166. TQ3Status                myStatus;
  167. TQ3ViewStatus            myViewStatus;
  168.  
  169.                 /* START RENDERING */
  170.                                 
  171.     myStatus = Q3View_StartRendering(gMyViewObject);            // start rendering
  172.     if ( myStatus == kQ3Failure )
  173.         DoFatalAlert("\p Q3View_StartRendering Failed!");
  174.  
  175.     do
  176.     {
  177.                 /* SET INTERPOLATION STYLE */
  178.  
  179.         myStatus = Q3Style_Submit(gMyStyleObject_Interpolation,gMyViewObject);
  180.         if ( myStatus == kQ3Failure )
  181.             DoFatalAlert("\p Q3Style_Submit Failed!");
  182.             
  183.                 /* SET BACKFACING STYLE */
  184.                     
  185.         myStatus = Q3Style_Submit(gMyStyleObject_Backfacing,gMyViewObject);
  186.         if ( myStatus == kQ3Failure )
  187.             DoFatalAlert("\p Q3Style_Submit Failed!");
  188.             
  189.             
  190.                     /* SET FILL STYLE */
  191.                     
  192.         myStatus = Q3Style_Submit(gMyStyleObject_Fillstyle, gMyViewObject);
  193.         if ( myStatus == kQ3Failure )
  194.             DoFatalAlert("\p Q3Style_Submit Failed!");
  195.             
  196.             
  197.                     /* SET SHADER TO USE */
  198.                     
  199.         myStatus = Q3Shader_Submit(gMyShaderObject_IlluminationShader, gMyViewObject);
  200.         if ( myStatus == kQ3Failure )
  201.             DoFatalAlert("\p Q3Shader_Submit Failed!");
  202.             
  203.             
  204.                 /* DRAW THE MESH OBJECT */
  205.                 
  206.         myStatus = Q3Geometry_Submit(gMyMeshObject,gMyViewObject);
  207.         if ( myStatus == kQ3Failure )
  208.             DoFatalAlert("\p Drawing Mesh Failed!");
  209.         
  210.         
  211.                     /* SEE IF DONE */
  212.                     
  213.         myViewStatus = Q3View_EndRendering(gMyViewObject);
  214.         
  215.     } while ( myViewStatus == kQ3ViewStatusRetraverse );
  216. }
  217.  
  218.  
  219.  
  220.